﻿namespace Harness.Commands
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using mshtml;
    using SHDocVw;

    internal class SetFocusCommand : ICommand
    {
        #region Constants

        private const int LocatorArgIndex = 0;

        #endregion

        #region Constants

        public void SyntaxCheck(IHarness harness, IEnumerable<object> args)
        {
            if (null == args.ElementAtOrDefault(LocatorArgIndex))
            {
                throw new ArgumentNullException((LocatorArgIndex + 1).ToString() + ":Locator");
            }
        }

        public dynamic Execute(IHarness harness, IEnumerable<object> args)
        {
            string locator = (args.ElementAtOrDefault(LocatorArgIndex) ?? string.Empty).ToString();

            IHTMLElement element = IE.Element.FindIfThrowNotFound(locator, IE.ActiveDocument);
            harness.HighlightElement(element, "setFocus");
            IE.Element.SetFocus(element);

            return null;
        }

        #endregion
    }
}
